var loadjscssfile = function(filename, filetype) {
	if(filetype == "js") {
		var fileref = document.createElement("script");
		fileref.setAttribute("type", "text/javascript");
		fileref.setAttribute("src", filename);
	} else if(filetype == "css") {
		var fileref = document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
	}
	if(typeof fileref != "undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}
};
//封装Ajax开始
var host = "http://47.92.130.190/";
$.zcypost = function(api, data, successfn) {
	data = (data == null || data == "" || typeof(data) == "undefined") ? {
		"date": new Date().getTime()
	} : data;
	$.ajax({
		type: "post",
		data: data,
		url: host + api,
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(d) {
			if(d.code == 1) {
				successfn(d);
			} else {
				mui.toast(d.message);
			}
		},
		error: function(e) {
			mui.toast('AJAX请求出错');
		}
	});
};
$.zcyget = function(api, successfn) {
	data = (data == null || data == "" || typeof(data) == "undefined") ? {
		"date": new Date().getTime()
	} : data;
	$.ajax({
		type: "get",
		url: host + api,
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(d) {
			if(d.code == 1) {
				successfn(d);
			} else {
				mui.toast('出现错误：' + d.message);
			}

		},
		error: function(e) {
			mui.toast('AJAX请求出错');
		}
	});
};
$.zcyget = function(api, data, successfn) {
	data = (data == null || data == "" || typeof(data) == "undefined") ? {
		"date": new Date().getTime()
	} : data;
	$.ajax({
		type: "get",
		url: host + api,
		dataType: "JSON",
		data: data,
		xhrFields: {
			withCredentials: true
		},
		success: function(d) {
			if(d.code == 1) {
				successfn(d);
			} else {
				mui.toast('出现错误：' + d.message);
			}

		},
		error: function(e) {
			mui.toast('AJAX请求出错');
		}
	});
};
//封装Ajax结束
//自定义的一些东西

$(function() {

	window.addEventListener('tap', function(e) {
		//class包含zcy-login-herf 点击自动跳转到login.html
		if($(e.target).hasClass('zcy-login-herf')) {
			location.href = ('login.html')
			//$.targets.action = false;
			return true;
		}
		if($(e.target).hasClass('zcy-register-herf')) {
			location.href = ('register.html')
			//$.targets.action = false;
			return true;
		}
	});
})
//密码框小眼睛控制开始
function showPassWord(passwordId, show) {　　
	//	if(typeof jQuery == 'undefined') {
	//		setTimeout(function() {
	//			loadjscssfile("js/jquery-2.1.0.js", "js");
	//		}, 200)
	//
	//	} else {
	//
	//	}

	if($("#" + passwordId).attr("type") == "password") {
		$("#" + passwordId).attr("type", "text");
		$(show).attr("src", "images/login/睁眼.png");
	}　
	else {
		$("#" + passwordId).attr("type", "password");
		$(show).attr("src", "images/login/闭眼.png");
	}　
}
//密码框小眼睛控制结束
//发送短信验证码开始
var wait = 60;

function sendSmsCode(phoneNumId, o, type) {

	var phoneObj = document.getElementById(phoneNumId);
	if(phoneObj.value == "") {
		mui.toast('请输入正确的手机号码');
		return;
	}

	var phoneVal = phoneObj.value;
	var p1 = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
	var me = false;
	if(p1.test(phoneVal)) me = true;
	if(!me) {
		phoneObj.value = '';
		mui.toast('请输入正确的手机号码');
		phoneObj.focus();
		return false;
	}

	$.zcypost(type, {
		phone: phoneObj.value
	}, function(s) {
		mui.toast('已经发送了短信验证码,请注意查收');
	})

	updateTime(o);
}

function updateTime(o) {
	if(wait == 0) {
		o.removeAttribute("disabled");
		o.value = "获取验证码";
		wait = 60;
	} else {
		o.setAttribute("disabled", true);
		o.value = wait;
		wait--;
		setTimeout(function() {
				updateTime(o)
			},
			1000)
	}
}
//发送短信验证码结束
//自定义验证开始

function validate(requiredGroup) {
	var query;
	if(requiredGroup != undefined) {
		query = $('input[required="required"][requiredGroup=' + requiredGroup + ']');
	} else {
		query = $('input[required="required"]');
	}

	for(var i = 0; i < query.length; i++) {
		var jq = $(query[i]);
		var jqGroup = jq.attr("requiredGroup")

		if(jq.val() == "") {
			mui.toast(jq.attr("oninvalid"));

			return false;
		}
	}

	var query2 = $('input[zcySameWith]');
	for(var i = 0; i < query2.length; i++) {
		var jq = $(query2[i]);
		var pwd1 = jq.val();
		var pwd2 = $("#" + jq.attr('zcySameWith')).val();
		if(pwd1 != pwd2) {
			mui.toast("两次密码输入不符");
			return false;
		}
	}
	return true;
}

//自定义验证结束
//填充用户头像,用户名信息开始
function showUserInfo(divId) {
	var userName = "未获取到";
	var userNumber = "未获取到";
	try {
		var userInfo = JSON.parse($.cookie("userInfo"));
		userName = userInfo.name == undefined ? "未获取到" : userInfo.name;
		userNumber = userInfo.number == undefined ? "未获取到" : userInfo.number;
	} catch(e) {

	}

	var parentdiv = $('#' + divId);
	parentdiv.attr("id", "play-body")
	parentdiv.addClass("mui-content");
	parentdiv.addClass("mui-col-xs-11");

	var childdiv = $('<div class="user-info mui-col-xs-12 mui-row"></div>');

	var imageDiv = $('<div class="mui-col-xs-2"> <a href="" class="user-head-link img-btn"></a> </div>');

	var infoDiv = $('<div class="mui-col-xs-10"><div class=""><h3>' + userName + '</h3></div><div class="user-number"><span>' + userNumber + '</span></div></div>');

	imageDiv.appendTo(childdiv);
	infoDiv.appendTo(childdiv);
	childdiv.appendTo(parentdiv);

}
//填充用户头像,用户名信息结束
//计算距离中考的时间 传递1加年级,传递0减年级 ,传递其他,只计算
function getEntranceExamDate(group) {
	var cookiesValue = $.cookie("userGrade");
	//	if(cookiesValue == undefined) {
	//		cookiesValue = 1;
	//	}
	var userGrade = parseInt(cookiesValue);

	switch(group) {
		case 0:
			if(userGrade != 3) {
				userGrade += 1;
				$.zcypost("updateGrade", {
					grade: userGrade
				})
			}
			break;
		case 1:
			if(userGrade != 1) {
				userGrade -= 1;
				$.zcypost("updateGrade", {
					grade: userGrade
				})
			}
			break;
		default:
			break;
	}

	SetCookies("userGrade", userGrade);

	var data = new Date();
	data.setMonth(5);
	data.setDate(17);
	//判断一下,现在的时间,距离今年的中考的天数是正的还是负的.如果是负的,就需要计算明年的中考
	if(GetDateDiff(data) < 0) {
		data.setFullYear(data.getFullYear() + 1);
	}
	switch(userGrade) {
		case 1: //初一,加两年
			data.setFullYear(data.getFullYear() + 2);
			break;
		case 2:
			data.setFullYear(data.getFullYear() + 1);
			break;
		case 3:
			break;
		default:
			break;
	}
	return GetDateDiff(data);
}

function GetDateDiff(endDate) {

	var startTime = new Date();
	startTime.setFullYear(startTime.getFullYear());

	var dates = (startTime - endDate) / 1000 / 60 / 60 / 24;

	return parseInt(dates) * -1;
}

function AddYears(date, value) {
	date.setFullYear(date.getFullYear() + value);
}
//计算距离中考的时间 传递参数:年级 结束
request = function(keyValue) {
	var search = location.search.slice(1);
	var arr = search.split("&");
	for(var i = 0; i < arr.length; i++) {
		var ar = arr[i].split("=");
		if(ar[0] == keyValue) {
			if(unescape(ar[1]) == 'undefined') {
				return "";
			} else {
				return unescape(ar[1]);
			}
		}
	}
	return "";
}
$.zcyGetArrayById = function(id, array) {

	var newItem = array.find(function(item) {
		return item.id == id
	});

	console.log("New Data")
	return newItem;

}

function SetCookies(name, value) {
	$.cookie(name, value, {
		path: '/',
		expires: 10
	});
}